﻿using Abp.AspNetCore.Mvc.Controllers;
using Abp.Domain.Repositories;
using Batman.Cms.Core.Models.Mvc;
using Batman.Cms.Core.Utils;
using Batman.Core;
using Batman.Jianbei.Forum.Core.ChatRoom;
using Batman.Jianbei.Forum.Core.Consts;
using Batman.Jianbei.Forum.Core.Domains.Commons;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Batman.Jianbei.Forum.Areas.ChatRoom
{
    [Area(RouteNames.ChatRoom)]
    //[Authorize]
    public class MaskAlbumController : AbpController
    {
        public IRepository<MaskAlbum, string> albumRepo { get; set; }
        public IRepository<Image, string> imageRepo { get; set; }

        public readonly IHostingEnvironment _hostingEnvironment;

        public MaskAlbumController(IHostingEnvironment env)
        {
            _hostingEnvironment = env;
        }

        public IActionResult GetAlbums()
        {
            var albums = albumRepo.GetQuery().Where(t => t.Enabled).Select(t => new
            {
                t.Id,
                t.Name,
            }).ToList();

            return Json(albums);
        }

        [Authorize]
        public IActionResult UploadMaskImage()
        {
            var file = Request.Form.Files.FirstOrDefault();

            var result = ImageUtil.SaveImage(file, new FileInfo(file.FileName).Extension, _hostingEnvironment.ContentRootPath, out var success);
            if (!success) return Json(new ResponseBase(false, result));

            var image = imageRepo.Insert(new Image { CreateAt = DateTime.Now, CreateBy = User.GetUserId(), Url = result });

            return Json(image);
        }
    }
}
